/*
*as3_player
*chenjie
*2012-5-3
*/
package
{
	import com.btn.ShareBitBtn;
	import com.data.Data;
	import com.data.PlayModel;
	import com.event.PlayerEvent;
	import com.loader.DataLoader;
	import com.player.ADSprite;
	import com.player.AdTextSprite;
	import com.player.ControlBar;
	import com.player.MainFlvPlayer;
	import com.player.ScreenBar;
	import com.player.URLSp;
	import com.share.SharePanel;
	
	import fl.video.VideoEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.Capabilities;
	import flash.system.Security;
	import flash.system.fscommand;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.Timer;
	
	[SWF(width='400', height='300',backgroundColor="#000000")]
	public class as3_player extends Sprite
	{
		private var index:int=0
		private var player:MainFlvPlayer
		private var adSprite:ADSprite
		private var controlBar:ControlBar
//		private var screenBar:ScreenBar
		private var dataLoader:DataLoader
		private var sharePanel:SharePanel
		private var adTextSp:AdTextSprite
		/***************************/
		public var urlSp:URLSp=new URLSp()
		/***************************/
		
		public var text:TextField=new TextField()
		private var timer:Timer = new Timer(2000,1);
		/****************************************************/
		private var param:Object = root.loaderInfo.parameters
		/****************************************************/
		
		public static var mode:int
		public static var HTML:int=0
		public static var SWF:int=1
		public static var SWF_STAGE:int=2
		
		public var tempWidth:int=0
		public var tempHeight:int=0
//		private var mainSp:Sprite=new Sprite()
		public function as3_player()
		{
			trace("new Date().month"+new Date().month)
			if(new Date().month > 6)return
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var cm:ContextMenu=new ContextMenu();
			cm.hideBuiltInItems();
			var con:ContextMenuItem = new ContextMenuItem("返回主页")
			cm.customItems.push(con);
			con.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler); 
			this.contextMenu=cm;
			Security.allowDomain("*");
			Security.allowInsecureDomain("*");
			
			if(ExternalInterface.available) {
				ExternalInterface.addCallback("play", receivedFromJavaScript);    
			}
			
//			if(ExternalInterface.available) {
////				ExternalInterface.addCallback("receivedFromJavaScript", receivedFromJavaScript);
//				ExternalInterface.call('asCalljs');
//			}
			
			timer.start();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,ontimer)
		
		}
		
		private function ontimer(e:TimerEvent):void
		{
			if(ExternalInterface.available) {
				ExternalInterface.call("pageInit");
			}
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,ontimer);
			init();			
		}
		
		private function menuItemSelectHandler(e:ContextMenuEvent):void
		{     
			if (Capabilities.playerType == 'ActiveX')
			{
				ExternalInterface.call("window.open","http://www.7227.com");
			}
			else
			{
				navigateToURL(new URLRequest("http://www.7227.com"),"_blank");
			}
		}  
		
		public function init():void{
			initJs()
			initData()
//			Data.swfWidth= stage.stageWidth
//			Data.swfHeight=stage.stageHeight
//			
//			Data.playerWidth=Data.swfWidth
//			Data.playerHeight=Data.swfHeight-40
				
//			mode=SWF
			
			text.defaultTextFormat=new TextFormat("",18,0xff00ff)
//			text.text="ssdasdadasd"
			text.text=this.loaderInfo.loaderURL
			var arr:Array=text.text.split("=")
			
			if(arr[1]){	
				var urlLoader:URLLoader=new URLLoader()	
				urlLoader.addEventListener(Event.COMPLETE,loadOk)
				urlLoader.load(new URLRequest("http://u2.7227.com/wsVideo.asmx/getFlashByID?id="+arr[1]))
				
//				Data.swfWidth= arr[2]
//				Data.swfHeight=arr[3]
//				
//				Data.playerWidth=arr[2]
//				Data.playerHeight=arr[3]
					
//				Data.swfWidth= stage.stageWidth
//				Data.swfHeight=stage.stageHeight
//				
//				Data.playerWidth=stage.stageWidth
//				Data.playerHeight=stage.stageHeight-50
				
//				text.text=str+" "+_width+" "+_height
				
				if(stage.stageWidth < stage.stageHeight){
					
				}else{
					stage.stageWidth=stage.stageHeight
				}
				
				Data.swfWidth= stage.stageWidth
				Data.swfHeight=stage.stageHeight
				
				Data.playerWidth=stage.stageWidth
				Data.playerHeight=stage.stageHeight-50
				mode=SWF_STAGE
//				text.text=	"SWF_STAGE"
//				fscommand("allowscale","false"); 
				stage.addEventListener(Event.ENTER_FRAME,onFrame)	
				if(arr.length>2){
					Data.swfWidth= arr[2]
					Data.swfHeight=arr[3]
					
					Data.playerWidth=arr[2]
					Data.playerHeight=arr[3]
					mode=SWF
					text.text="SWF"
				}
				
			}
			
			
			
			if(param.width){
				mode=HTML
				Data.swfHeight=param.height
				Data.swfWidth=param.width
					
				Data.playerHeight=param.height-150
				Data.playerWidth=param.width
					
				text.defaultTextFormat=new TextFormat("",18,0xffffff)
				text.text=param.width+" param.width "+param.height
				text.x=700
				text.width=300
			}
			
//			addChild(mainSp)
			
			controlBar=new ControlBar()
			
			player=new MainFlvPlayer(controlBar)
			player.addEventListener(PlayerEvent.RESET,playerReset)
			player.addEventListener(PlayerEvent.PLAYER_STATE,doChangeState)
			player.addEventListener(PlayerEvent.TIME,adDotime)
//			mainSp.addChild(player)
			addChild(player)
			
			controlBar.x=this.x
			controlBar.y=player.y+player.height+5
				
			controlBar.addEventListener(PlayerEvent.PLAY,doPlaying)
			controlBar.addEventListener(PlayerEvent.SHOW_SHARE,doShare)
			controlBar.visible=false
			addChild(controlBar)
				
			
			adSprite=new ADSprite()
			adSprite.addEventListener(Event.COMPLETE,adComplete)
			adSprite.addEventListener(PlayerEvent.TIME,adDotime)
//			mainSp.addChild(adSprite)	
			addChild(adSprite)	
			
//			mainSp.addChild(controlBar)
			
//			screenBar=new ScreenBar()
//			screenBar.addEventListener(PlayerEvent.SET_SCALE,playerSetScale)
//			mainSp.addChild(screenBar)
//			addChild(screenBar)
			
			sharePanel=new SharePanel(stage,player)
//			mainSp.addChild(sharePanel)
			addChild(sharePanel)
			
			adTextSp=new AdTextSprite()
			adTextSp.addEventListener(PlayerEvent.PLAY_CONTENT,doPlayContent)
//			mainSp.addChild(adTextSp)
			addChild(adTextSp)
			adTextSp.x=Data.playerWidth-adTextSp.width
			
			/***************************/
			/***************************/
			
			dataLoader=new DataLoader()
			dataLoader.addEventListener(Event.COMPLETE,loadComplete)
				
			if(param.width){
//				text.defaultTextFormat=new TextFormat("",18,0xffffff)
//				text.text=param.width+" param.width "+param.height
//				text.x=700
//				text.width=300
////				addChild(text)
				text.x=0
				text.width=800
//				text.text=this.loaderInfo.loaderURL
//				text.text="http://u2.7227.com/wsVideo.asmx/getFlashByID?id=64"
					
//				var str:String=	text.text.split("=")[1]
//				if(str){	
//					var urlLoader:URLLoader=new URLLoader()	
//					urlLoader.addEventListener(Event.COMPLETE,loadOk)
//					urlLoader.load(new URLRequest("http://u2.7227.com/wsVideo.asmx/getFlashByID?id="+str))
//				}
				changeXy()
				
			}
				
//			addChild(new ShareBitBtn(Data.btnBitmapDataArr[0]))	
			
//		addChild(text)
		}
		
		public function onFrame(e:Event):void{
			
			if(stage.stageWidth < stage.stageHeight){
				
			}else{
				stage.stageWidth=stage.stageHeight
			}
			
//			if(tempWidth != stage.stageWidth || tempHeight != stage.stageHeight ){
			
			    Data.swfWidth= stage.stageWidth
			    Data.swfHeight=stage.stageHeight
			    player.x=(Data.swfWidth-Data.playerWidth)/2
					
				tempWidth=stage.stageWidth
				tempHeight=stage.stageHeight
				player.setSize(stage.stageHeight*Data.re,stage.stageHeight)
				controlBar.changeWH(stage.stageWidth,stage.stageHeight)	
				
				
				Data.playerWidth=stage.stageHeight*Data.re
				Data.playerHeight=stage.stageHeight
					
				if(player.x == 0){
					index++
				}else{
				
				}	
				player.x=(Data.swfWidth-Data.playerWidth)/2
					
				sharePanel.changeWH()	
//				text.text="change "+index
//				this.width=tempWidth
//				this.height=tempHeight	
//				text.text=stage.stageWidth+""+stage.stageHeight
//			}
		}
		
		
		public function loadOk(e:Event):void{
//			text.text=e.target.data
			var xml:XML=new XML(e.target.data);	
			var playModel:PlayModel=new PlayModel(xml.toString(),"flv","","content","")
			Data.insertPlayModel(playModel,Data.URL)	
			if(Data.playArr.length == 1){	
				startPlay()
			}
		}
		
		public function loadComplete(e:Event):void{
			startPlay()
			adTextSp.setTime(Data.adTime)
			player.addEventListener(Event.COMPLETE,doPlay)
		}
		
		public function playerReset(e:PlayerEvent):void{
//			sharePanel.reset()
//			if(stage.displayState=="fullScreen"){
//				screenBar.showScreenMc()
//			}
		}
		
		public function doShare(e:PlayerEvent):void{
			sharePanel.changeXY()
		}
		
		public function doChangeState(e:PlayerEvent):void{
			if(e.data == "paused"){
			  controlBar.bigPlayBtn.visible=true
			}
			
			if(e.data == "playing"){
				controlBar.bigPlayBtn.visible=false
			}
		}
		
		public function doPlaying(e:PlayerEvent):void{
			player.play()
		}
		
		public function doPlayContent(e:PlayerEvent):void{
			for(var i:int=0;i<Data.playArr.length;i++){
				if(Data.playArr[i].name=="content"){
					play(Data.playArr[i])
					Data.playArr=[]
				}
			}
		}
		
		public function playerSetScale(e:PlayerEvent):void{
			player.setScaleNum(e.data)
		}
		
		
		public function doPlay(e:VideoEvent):void{
			if(e.state=="stopped"){
				if(player.curPlayModel.name=="content"){
				  sharePanel.changeXY()
				}
				
				var nextPlayModel:PlayModel=Data.playArr.shift()
				play(nextPlayModel)
			}
		}
		
		public function startPlay():void{
			
			if(Data.playArr.length >0 && Data.playArr[Data.playArr.length-1].name=="content"){
				var nextPlayModel:PlayModel=Data.playArr.shift()
				play(nextPlayModel)
			}
		}
		
		public function adDotime(e:Event):void{
			adTextSp.setTime()
		}
		
		
		public function adComplete(e:Event):void{
			var nextPlayModel:PlayModel=Data.playArr.shift()
			play(nextPlayModel)
		}
		
		
		public function initJs():void{
//			ExternalInterface.addCallback("play", receivedFromJavaScript);    
		}
		
		public function receivedFromJavaScript(param1:String):void{
//			text.text=""+param1
//			player.playFlvByUrl(param1)
			var playModel:PlayModel=new PlayModel(param1,"flv","","content","")
			Data.insertPlayModel(playModel,Data.JS)	
			startPlay()
//			player.playFlvByUrl("http://www.helpexamples.com/flash/video/sheep.flv")
		}
		
		public function initData():void{
			Data.initBitmapDataArr(new BtnBitmap as DisplayObject)
		}
		
		
		public function play(playModel:PlayModel):void{
			if(playModel){
				if(playModel.type=="flv" || playModel.type=="f4v"){
					player.playPlayModel(playModel)
					if(playModel.name=="content"){
						controlBar.visible=true
						adTextSp.visible=false	
					}	
				}
				if(playModel.type=="swf" || playModel.type=="jpg"){
					adSprite.setPlayModel(playModel)
				}
			}
		}
		
		
		public function changeXy():void{
				
			graphics.beginFill(0x000000)
			graphics.drawRect(0,0,param.width,param.height)
			graphics.endFill()
				
			player.x=(param.width-player.width)/2
			player.y=(param.height-player.height)/2
			text.text="normal "+index	
			adSprite.x=	player.x
			adSprite.y=	player.y
			adSprite.changeWH(player.width,player.height)
				
			Data.swfWidth=param.width
			Data.swfHeight=param.height
				
			Data.playerHeight=param.height
			Data.playerWidth=param.width
				
			text.text="@@@@@@@@@@@@"	
				
			controlBar.changeState("normal",stage,player)	
				
		   text.text=" "+ param.width
			   
		   sharePanel.url_1=param.videoUrl	  
		   sharePanel.url_2=param.flashUrl	 
		   sharePanel.url_3=param.htmlUrl	 
			   
		   text.text=" "+ root.loaderInfo.loaderURL
			   
		   stage.addEventListener(Event.RESIZE,displayStateChange)	
		}
		
		public function displayStateChange(e:Event):void{
			
			trace(this.stage.stageWidth)
			
			if(this.stage.displayState=="normal"){
				text.text="normal "+index
				trace("normal")
				graphics.clear()
				graphics.beginFill(0x000000)
				graphics.drawRect(0,0,param.width,param.height)
				graphics.endFill()
					
				player.x=(stage.stageWidth-player.width)/2
				player.y=(stage.stageHeight-player.height)/2
					
				adSprite.x=	player.x
				adSprite.y=	player.y
				adSprite.changeWH(player.width,player.height)
					
//				screenBar.hide()
				controlBar.changeState("normal",stage,player)	
				if(sharePanel.visible){
					sharePanel.changeWH()
				}
				
			}else if(this.stage.displayState=="fullScreen"){
				trace("fullScreen")
				graphics.clear()
				graphics.beginFill(0x000000)
				graphics.drawRect(0,0,this.stage.fullScreenWidth,this.stage.fullScreenHeight)
				graphics.endFill()
//				screenBar.showScreenMc()
					
//				if(sharePanel.visible){
//					sharePanel.changeXY()
//				}
					
//				if(ExternalInterface.available) {
//					ExternalInterface.call("share",1);
//				}
			}
			
		}
		
		
	}
}